document.ready = function() {

	var geonames = {};
     geonames.baseURL = "http://ws.geonames.org/";
     geonames.method = "findNearbyWikipediaJSON";
     
     $('#getWiki').click(function(){
          if (navigator.geolocation) {
               navigator.geolocation.getCurrentPosition(onSuccess, onError);
          } else {
               // If location is not supported on this platform, disable it
               $('#getWiki').value = "Geolocation not supported";
               $('#getWiki').unbind('click');
          }
     });
     
     
     geonames.search = function(lat,lng){
 
     // get the data in JSON format from Geonames
     $.getJSON(geonames.baseURL + geonames.method + '?formatted=true&amp;lat=' + lat + '&amp;lng=' + lng + '&amp;style=full&amp;radius=10&amp;maxRows=25',function(data){
          // Loop through each item in the result and add it to the DOM
          $.each(data.geonames, function() {
               $('')
               .hide()
               .append('<a href="http://'+this.wikipediaUrl+'"><h2>'+this.title+'</h2></a>'+ this.summary + '<span class="ui-li-aside"><h5>'+this.distance+' (km)</h5></span>')
               .appendTo('#wikilist')
               .show();
          });
          // Once the data is added to the DOM, make the transition
          //$.mobile.changePage('#dashboard',"slide",false,true);
 
          // refresh the list to make sure the theme applies properly
          $('#wikilist').listview('refresh');
     });
};

		// Success function for Geolocation call
	function onSuccess(position)
	{
	     geonames.search(position.coords.latitude,position.coords.longitude);
	}
	 
	// Error function for Geolocation call
	function onError(msg)
	{
	     alert(msg);
	}

	try {

		//GET ALL
		$.ajaxSetup({
			url : "masterController.php",
			type : "GET",
			headers : {
				'Accept' : "application/json"
			}
		});

		var myYr;
		var map_canvas;

		function updateStatus(message) {
			document.getElementById("status").innerHTML = message;
		}

		function loadDemo() {
			if(navigator.geolocation) {
				updateStatus("");
				//Tom sträng när status är OK
				navigator.geolocation.watchPosition(updateLocation, handleLocationError, {
					maximumAge : 20000

				});
			}
		}


		window.addEventListener("load", loadDemo, true);

		function handleLocationError(error) {
			switch (error.code) {
				case 0:
					updateStatus("Nu blev det ett litet fel när Er lokalalisation skulle hämtas... " + error.message);
					break;

				case 1:
					updateStatus("Användaren förhindrade att sidan hämtade lokalalisation");
					break;

				case 2:
					updateStatus("Webbläsaren kunde inte avgöra Er lokalalisation... " + error.message);
					break;

				case 3:
					updateStatus("Webbläsaren slocknade innan lokalisationen hämtades");
					break;
			}
		}

		function updateLocation(position) {
			var latitude = position.coords.latitude;
			var longitude = position.coords.longitude;
			var accuracy = position.coords.accuracy;
			var timestamp = position.timestamp;

			document.getElementById("latitude").innerHTML = latitude;
			document.getElementById("longitude").innerHTML = longitude;
			//document.getElementById("accuracy").innerHTML = accuracy;
			//document.getElementById("timestamp").innerHTML = timestamp;

			try {
				var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
				//Ny google map med idet plus longitude och latitude
				var myOptions = {
					zoom : 10,
					center : latlng,
					mapTypeId : google.maps.MapTypeId.ROADMAP
				};
				var map = new google.maps.Map(document.getElementById("map"), myOptions);
				//myOptions är zoom center osv.
				$('#map').append(map);
				//Hänger på kartan på min DIV med namn map

				var marker = new google.maps.Marker({
					position : latlng,
					map : map,
					title : "Här är Ni!"
				});
				marker.setMap(map);
				// kopplar marker med kartan

			} catch(e) {
				$("#map").append('<p>Karta kunde inte hämtas</p>');
				$("#map").empty();
				return;
			}
		}

		//navigator.geolocation.getCurrentPosition(updateLocation, handleLocationError);

	} catch (e) {
		$("#list").append('<p>Ett fel inträffade!</p>');
		return;
	}

}